package com.ruoyi.carbon.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.carbon.domain.BidBiddingFiles;
import com.ruoyi.carbon.mapper.BidBiddingMapper;
import com.ruoyi.carbon.domain.BidBidding;
import com.ruoyi.carbon.service.IBidBiddingService;

/**
 * 招投标Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class BidBiddingServiceImpl implements IBidBiddingService 
{
    @Autowired
    private BidBiddingMapper bidBiddingMapper;

    /**
     * 查询招投标
     * 
     * @param id 招投标主键
     * @return 招投标
     */
    @Override
    public BidBidding selectBidBiddingById(Long id)
    {
        return bidBiddingMapper.selectBidBiddingById(id);
    }

    /**
     * 查询招投标列表
     * 
     * @param bidBidding 招投标
     * @return 招投标
     */
    @Override
    public List<BidBidding> selectBidBiddingList(BidBidding bidBidding)
    {
        return bidBiddingMapper.selectBidBiddingList(bidBidding);
    }

    /**
     * 新增招投标
     * 
     * @param bidBidding 招投标
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBidBidding(BidBidding bidBidding)
    {
        bidBidding.setCreateTime(DateUtils.getNowDate());
        int rows = bidBiddingMapper.insertBidBidding(bidBidding);
        insertBidBiddingFiles(bidBidding);
        return rows;
    }

    /**
     * 修改招投标
     * 
     * @param bidBidding 招投标
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBidBidding(BidBidding bidBidding)
    {
        bidBidding.setUpdateTime(DateUtils.getNowDate());
        bidBiddingMapper.deleteBidBiddingFilesByBidId(bidBidding.getId());
        insertBidBiddingFiles(bidBidding);
        return bidBiddingMapper.updateBidBidding(bidBidding);
    }

    /**
     * 批量删除招投标
     * 
     * @param ids 需要删除的招投标主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBidBiddingByIds(Long[] ids)
    {
        bidBiddingMapper.deleteBidBiddingFilesByBidIds(ids);
        return bidBiddingMapper.deleteBidBiddingByIds(ids);
    }

    /**
     * 删除招投标信息
     * 
     * @param id 招投标主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBidBiddingById(Long id)
    {
        bidBiddingMapper.deleteBidBiddingFilesByBidId(id);
        return bidBiddingMapper.deleteBidBiddingById(id);
    }

    /**
     * 新增招标信息附件信息
     * 
     * @param bidBidding 招投标对象
     */
    public void insertBidBiddingFiles(BidBidding bidBidding)
    {
        List<BidBiddingFiles> bidBiddingFilesList = bidBidding.getBidBiddingFilesList();
        Long id = bidBidding.getId();
        if (StringUtils.isNotNull(bidBiddingFilesList))
        {
            List<BidBiddingFiles> list = new ArrayList<BidBiddingFiles>();
            for (BidBiddingFiles bidBiddingFiles : bidBiddingFilesList)
            {
                bidBiddingFiles.setBidId(id);
                list.add(bidBiddingFiles);
            }
            if (list.size() > 0)
            {
                bidBiddingMapper.batchBidBiddingFiles(list);
            }
        }
    }
}
